package thesis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.aliasi.util.Pair;
import com.mongodb.DBObject;

public class Summary {
	private List<DBObject> summary = new ArrayList<DBObject>();
	private ArrayList<DataObject> memoryTweets = new ArrayList<DataObject>();
	private double quality;
	private double diversity;
	private double coverage;
	private double score;

	public void addMemoryTweet(DataObject t) {
		memoryTweets.add(t);
	}

	public String toString() {
		String result = "";
		for (DataObject t : memoryTweets) {
			result += "\n" + t.getDbId();
			//result += "<< ";
			// for (Pair<Double, InfoUnit> pair : t.getUnits()) {
			// result += pair.b();
			// }
			//result += "  >>";
		}
		return result;
	}

	public Summary() {

	}

	public void memory2DB() {
		this.summary.clear();
		for (DataObject t : this.memoryTweets) {
			DBObject tweet = FSModule.getTweetWithTweetId(t.getDbId());
			this.summary.add(tweet);
		}
	}

	public void computeScore(double alpha, double beta, double gamma,
			ArrayList<InfoUnit> units, double[][] probs,
			HashMap<Long, DataObject> tweets) {
		if (probs.length > 0) {
			// compute quality
			quality = 0;
			for (DataObject t : this.memoryTweets) {
				quality += t.getQuality();
			}
			quality /= this.size();
			// compute diversity
			diversity = Double.MAX_VALUE;
			for (int i = 0; i < this.memoryTweets.size() - 1; i++) {
				for (int j = i + 1; j < this.memoryTweets.size(); j++) {
					double dist = DataUtil.dist(this.memoryTweets.get(i),
							this.memoryTweets.get(j));
					if (dist < diversity) {
						diversity = dist;
					}
				}
			}
			// compute coverage
			double[] covProb = new double[probs[0].length];
			for (int i = 0; i < covProb.length; i++) {
				covProb[i] = 1;
				for (DataObject t : this.memoryTweets) {
					covProb[i] *= 1 - probs[t.getInternId()][i];
				}
				covProb[i] = 1 - covProb[i];
			}
			coverage = this.size();
			for (DataObject t : tweets.values()) {
				double totalInfo = 0, covInfo = 0;
				for (int i = 0; i < covProb.length; i++) {
					totalInfo += probs[t.getInternId()][i]
							* units.get(i).getWeight();
					covInfo += probs[t.getInternId()][i]
							* units.get(i).getWeight() * covProb[i];
				}
				if (totalInfo > 0) {
					coverage += covInfo / totalInfo;
				}
			}
			coverage /= (double) (this.size() + tweets.size());
			score = alpha * quality + beta * diversity + gamma * coverage;
		}
	}

	// GETTERS AND SETTERS
	public double getQuality() {
		return quality;
	}

	public void setQuality(double quality) {
		this.quality = quality;
	}

	public double getDiversity() {
		return diversity;
	}

	public void setDiversity(double diversity) {
		this.diversity = diversity;
	}

	public double getCoverage() {
		return coverage;
	}

	public void setCoverage(double coverage) {
		this.coverage = coverage;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public ArrayList<DataObject> getMemoryTweets() {
		return memoryTweets;
	}

	public int size() {
		return memoryTweets.size();
	}

	public List<DBObject> getTweets() {
		return summary;
	}
}
